﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for ProRequest
/// </summary>
public class ProRequest
{
    public ProRequest()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataSet LoadRequestDetail(String strRequestCode)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();

        ds = db.getRequestDetailList(strRequestCode);

        return ds;
    }

    public DataSet LoadBusCat()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListBusCat();
        return ds;
    }
    public DataSet LoadBusCat(String strIdCatBus)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListBusCat(strIdCatBus);
        return ds;
    }
    //PIC is Leader, Manager, Member
    public DataSet LoadPic()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListPic();
        return ds;
    }
    public DataSet LoadPic(String strIdPic)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListPic(strIdPic);
        return ds;
    }
    public DataSet LoadDirector()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListDirector();
        return ds;
    }
    public DataSet LoadDirector(String strIdDirector)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListDirector(strIdDirector);
        return ds;
    }
    public DataSet LoadVPre()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListVPre();
        return ds;
    }
    public DataSet LoadVPre(String strIdVPre)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListVPre(strIdVPre);
        return ds;
    }
    public DataSet LoadListPartner()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListPartner();
        return ds;
    }
    public DataSet LoadListPartner(String strIdPartner)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getListPartner(strIdPartner);
        return ds;
    }
    public DataSet LoadSubmitter(String strUserName)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.isExistUser(strUserName);
        return ds;
    }

    public String GenerateRequestCode()
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        String strCurrentDate = MakeCurrentDateString();
        String strRequestCode = "";
        ds = db.getAllLastedRequests(strCurrentDate);
        if (ds != null & ds.Tables[0].Rows.Count > 0)
            strRequestCode = "RF-" + strCurrentDate + "-" +
                (ds.Tables[0].Rows.Count < 9 ? "00" : "0").ToString() +
                (ds.Tables[0].Rows.Count + 1).ToString();
        else if (ds != null && ds.Tables[0].Rows.Count == 0)
            strRequestCode = "RF-" + strCurrentDate + "-" + "001";
        else
            strRequestCode = "";
        return strRequestCode;
    }

    //YYYYMMDD
    private String MakeCurrentDateString()
    {
        String strResult = "";
        strResult += DateTime.Now.Year.ToString();
        strResult += (DateTime.Now.Month < 10 ? ("0" + DateTime.Now.Month.ToString()) : DateTime.Now.Month.ToString());
        strResult += (DateTime.Now.Day < 10 ? ("0" + DateTime.Now.Day.ToString()) : DateTime.Now.Day.ToString());
        return strResult;
    }

    public Boolean AddRequestInfo(RequestInfo rq)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.saveRequest(rq);
        return bResult;
    }

    public Boolean UpdateRequestInfo(RequestInfo rq)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.updateRequestInfo(rq);
        return bResult;
    }

    public Boolean SaveRequestDetail(RequestDetailEntity rqd)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.saveRequestDetail(rqd);
        return bResult;
    }
    public Boolean UpdateRequestDetail(RequestDetailEntity rqd)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.updateRequestDetail(rqd);
        return bResult;
    }
    public Boolean UpdateRequestConfrim(int iStatus, String strRequestCode, int iCodeOfRequestDetail)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.updateRequestConfirmStatus(iStatus, strRequestCode, iCodeOfRequestDetail);
        return bResult;
    }
    public Boolean DeleteRequestDetail(RequestDetailEntity rqd)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.deleteRequestDetail(rqd);
        return bResult;
    }
    public DataSet LoadRequestInfo(String strRequestCode)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getRequestInfo(strRequestCode);
        return ds;
    }

    public DataSet LoadRequestInfoToConfirm(String strConfirmBy, int intStatus)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getRequestInfo(intStatus, strConfirmBy);
        return ds;
    }

    public DataSet LoadRequestConfirmInfo(String strRequestCode)
    {
        ConnectDB db = new ConnectDB();
        DataSet ds = new DataSet();
        ds = db.getRequestConfirm(strRequestCode);
        return ds;
    }

    public Boolean CreateConfirmList(String strRequestCode, String strPIC, String strDirector, String strVPre)
    {
        int id = 1;
        CreateConfirmBy(strRequestCode, strPIC, id++, Constant.REQUEST_STATUS_PENDING);
        CreateConfirmBy(strRequestCode, strDirector, id++, Constant.REQUEST_STATUS_NONE);
        CreateConfirmBy(strRequestCode, strVPre, id, Constant.REQUEST_STATUS_NONE);
        return true;
    }

    public Boolean CreateConfirmBy(String strRequestCode, String strConfirmBy, int id, int iResult)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.addConfirmBy(strRequestCode, strConfirmBy, id, iResult, DateTime.Now);
        if (bResult)
            return true;
        else
            return false;
    }

    public Boolean UpdateConfirmList(String strRequestCode)
    {
        int id = 1;
        UpdateConfirmBy(strRequestCode, id++, Constant.REQUEST_STATUS_PENDING);
        UpdateConfirmBy(strRequestCode, id++, Constant.REQUEST_STATUS_NONE);
        UpdateConfirmBy(strRequestCode, id, Constant.REQUEST_STATUS_NONE);
        return true;
    }

    public Boolean UpdateConfirmBy(String strRequestCode,  int id, int iResult)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.updateConfirmBy(strRequestCode, id, iResult, DateTime.Now);
        if (bResult)
            return true;
        else
            return false;
    }

    public Boolean UpdateRequestInfoStatus(String strRequestCode, int iStatus)
    {
        ConnectDB db = new ConnectDB();
        Boolean bResult = db.updateRequestInfoStatus(strRequestCode, iStatus);
        if (bResult)
            return true;
        else
            return false;
    }
}
